//
//  SYComposeViewController.swift
//  SYWeibo
//
//  Created by Eric on 16/7/27.
//  Copyright © 2016年 Eric. All rights reserved.
//

import UIKit

class SYComposeViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
    }
    // MARK: - 设置视图
    private func setupUI(){
        view.backgroundColor = UIColor.whiteColor()
        setupNav()
        //添加控件
        view.addSubview(composeTextView)
        composeTextView.addSubview(composePictureView)
        view.addSubview(composeToolbarView)
        
        //设置约束
        composeTextView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
        composePictureView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: SCREENW - 20, height: SCREENW - 20))
            make.top.equalTo(composeTextView).offset(100)
            make.centerX.equalTo(composeTextView)
            
        }
        composeToolbarView.snp_makeConstraints { (make) -> Void in
            make.height.equalTo(44)
            make.leading.trailing.bottom.equalTo(view)  
        }
        //监听按钮点击
        composeToolbarView.closure = {[weak self](type: SHComposeToolbarViewType)->() in
            
            switch type{
            case .Picture:
                print("图片")
                self?.selectImage()
            case .Mention:
                print("@")
            case .Trend:
                print("#")
            case .Emoticon:
                print("表情")
            case .Add:
                print("+")
            
            
            }

        }

        // 监听配图中加号cell点击 弹出相册
        composePictureView.closure = {[weak self] in
            self?.selectImage()
        }
    }
    // MARK: - 设置导航
    private func setupNav(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", imgName: nil, target: self, action: "cancelAciton")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发布", imgName: nil, target: self, action: "sendAciton")
        //添加title
        navigationItem.titleView = titleView
        
    }
    //取消 点击按钮事件
    func cancelAciton(){
        dismissViewControllerAnimated(true, completion: nil)
    }
    //发布 点击按钮事件
    func sendAciton(){
        print("触发发布按钮")
        
    }
    
    //MARK :  -懒加载控件
    private lazy var titleView: UILabel = {
        // 用户名字
        let name = SYUserAccountViewModel.sharedTools.userAccount?.screen_name ?? ""
        //拼接字符串
        let str = "发微博\n\(name)"
        // 创建一个可变的富文本
        let attr = NSMutableAttributedString(string: str)
        //截取字符串
        let range = (str as NSString).rangeOfString(name)
        //设置富文本
        attr .addAttributes([NSForegroundColorAttributeName:ThemeColor, NSFontAttributeName: UIFont.systemFontOfSize(12)], range: range)
        let lab = UILabel()
        lab.numberOfLines = 0
        lab.textAlignment = .Center
        //富文本格式
        lab.attributedText = attr
        lab.sizeToFit()
        return lab
    }()
    
    //MARK :  -自定义titleView
    private lazy var composeTextView: SYComposeTextView = {
        let view = SYComposeTextView()
        view.delegate = self
        view.font = UIFont.systemFontOfSize(15)
        view.placeholder = "想说点什么吗?亲!"
        view.alwaysBounceVertical = true
        return view
    }()

    // 配图
    private lazy var  composePictureView: SYComposePictureView = SYComposePictureView()
    // 底部视图
    private lazy var  composeToolbarView: SYComposeToolbarView = SYComposeToolbarView()

}
// MARK: - UITextViewDelegate
extension SYComposeViewController: UITextViewDelegate{
    func textViewDidChange(textView: UITextView) {
        //发布按钮如果有值 就可以点击
        navigationItem.rightBarButtonItem?.enabled = textView.hasText()
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        // 取消第一响应
        self.composeTextView.resignFirstResponder()
    }
}
// 底部视图按钮点击回调
extension SYComposeViewController {
    func selectImage(){
        //从本地照片中获取图片和视频等文件
       let pickerVC = UIImagePickerController()
        //设置代理
        pickerVC.delegate = self
        self.presentViewController(pickerVC, animated: true, completion: nil)
        
        
    }
}

//MARK :  - 设置代理  得到图片   用画图的方式  压缩图片
//设置 UIImagePickerControllerDelegate UINavigationControllerDelegate
extension SYComposeViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        
//        let result = getImageScale(image, width: 600)
//        let data = UIImagePNGRepresentation(result)
//        let result = image.getImageScale(600)
//        let data = UIImagePNGRepresentation(result)
//        data?.writeToFile("/Users/Eric/Desktop/llalall", atomically: true)
        
        composePictureView.addImages(image.getImageScale(600))
        //设置背景图片 
//        composeTextView.backgroundColor = UIColor(patternImage: image)
        //程序员手动 关闭
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
    
    
//    //开启图片上下文来获取压缩图片
//    func getImageScale(image: UIImage , width: CGFloat) -> UIImage{
//        
//        // 压缩图片
//        //    /*
//        //        指定宽度来压缩
//        //       宽 1200    指定宽度  600
//        //       高 600              x
//        //    */
//        //如果当前图片的宽度 小于程序员指定的宽度
//        if image.size.width < width {
//            return image
//        }
//        //获取图片的最终高度
//        let height = (image.size.width * width) / image.size.width
//        //设置 图片的 rect
//        let rect = CGRect(x: 0, y: 0, width: width, height: height)
//        //01 开启图片上下文
//        UIGraphicsBeginImageContext(rect.size)
//        //02 渲染图片到上下文
//        image.drawInRect(rect)
//        //03 从上下文获取image
//        let result = UIGraphicsGetImageFromCurrentImageContext()
//        // 04 关闭图片上下文
//        UIGraphicsEndImageContext()
//        
//        return result
//    }
    
    
}









